// pages/index1/index1.js

var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    primarySize: 'default',
    disabled: false,
    plain: false,
    loading: false,

    icon: '../../images/logo.png',
    ulifeUrl: app.myUlife.ulifeUrl,//服务器路径
    stus:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
   
  },


  intologin: function () {
    var that = this;
    that.setData({
      disabled: true,
      loading: true
    })
    wx.clearStorageSync();
    that.wxWsqlogin();
  },


  // 登录start(未获取获取权限的登录)
  wxWsqlogin: function () {
    var that = this;
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        that.code = res.code;
        wx.setStorageSync('city', "未授权登录");
        wx.setStorageSync('province', "未授权登录");
        wx.setStorageSync('gender', "未授权登录");
        wx.setStorageSync('nickName', "未授权登录");
        wx.setStorageSync('avatarUrl', that.data.ulifeUrl + '/wx/defaultimg.jpg');
        that.getwxuserinfo();
      }
    })
  },




  // 发送 res.code 到后台换取 openId, sessionKey, unionId
  getwxuserinfo: function () {
    var that = this;
    wx.request({ 
      url: app.myUlife.ulifeUrl + '/wechart/getwxandinguserinfo.html',//请求地址
      data: {
        code: that.code
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var openid = res.data.openid;
        wx.setStorageSync('openid', openid);//加入缓存
        if (res.data.user_type != null && res.data.user_type != undefined && res.data.user_type != "" ){
          var usrinfo = res.data;
          wx.setStorageSync('user_type', usrinfo.user_type);
          //如果登录账号不为空 则登录
          //1学生 2教师
          if (usrinfo.user_type=="1"){
            if (usrinfo.stusize && usrinfo.stusize!="1"){
              that.setData({
                stus: usrinfo.stus
              });  
              //有多个孩子，选择其中一个登陆
              wx.navigateTo({
                url: '../../pages/qhdl/qhdl'
              })
            }else{
              wx.setStorageSync('name', usrinfo.name);
              wx.setStorageSync('identity', usrinfo.identity);
              wx.setStorageSync('nj', usrinfo.nj);
              wx.setStorageSync('bj', usrinfo.bj);
              wx.setStorageSync('usid', usrinfo.usid);
              wx.setStorageSync('dlzh', usrinfo.dlzh);
              wx.setStorageSync('sex', usrinfo.sex);
              wx.switchTab({
                url: '../../pages/home/home'
              })
            }
          }else{
            wx.setStorageSync('name', usrinfo.name);
            wx.setStorageSync('identity', usrinfo.identity);
            wx.setStorageSync('nj', usrinfo.nj);
            wx.setStorageSync('bj', usrinfo.bj);
            wx.setStorageSync('usid', usrinfo.usid);
            wx.setStorageSync('dlzh', usrinfo.dlzh);
            wx.setStorageSync('sex', usrinfo.sex);
            wx.switchTab({
              url: '../../pages/home/home'
            })
          }
        }else{
          //如果登录账号为空 跳转到选择登录方式
          wx.redirectTo({
            url: '../../pages/choice/choice'
          })
        }
      }
    })
  },


})
